package Control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.captcha.Captcha;
import nl.captcha.gimpy.DropShadowGimpyRenderer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.producer.DefaultTextProducer;

/**
 * Servlet implementation class CaptchaServlet
 */
public class CaptchaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public CaptchaServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  try {
	            Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
	//200, 50 is the size, 6 is the number of letters to use, we choose the shadow renderer.
	            response.setHeader("Cache-Control", "no-store");
	            response.setHeader("Pragma", "no-cache");
	            response.setDateHeader("Expires", 0);
	// don't cache it , don't store it, expire after 0 second, so with refresh new image loaded
	            response.setContentType("image/jpeg");
	//image type
	            CaptchaServletUtil.writeImage(response, captcha.getImage());
	//write the image
	            request.getSession().setAttribute("CorrectAnswer", captcha.getAnswer());
	//store the answer for this in session
	        } catch (Exception e) {
	            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
	            return;
	        }
	    }
		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		// TODO Auto-generated method stub
	}

}
